package com.jiang.books.service.jpa;

import com.google.common.collect.Lists;
import com.jiang.books.domain.User;
import com.jiang.books.repository.IUserRepository;
import com.jiang.books.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import java.math.BigInteger;
import java.util.List;

@Service("jpaUserService")
@Transactional
public class UserSpDataService implements IUserService {

    @Autowired
    IUserRepository repository;

    @PersistenceContext
    public EntityManager em;

    @Override
    @Transactional(readOnly = true)
    public List<User> findAll() {
        Iterable<User> ugs = repository.findAll();
        return Lists.newArrayList(ugs);
    }

    @Override
    @Transactional(readOnly = true)
    public User findById(BigInteger id) {
        return repository.findOne(id);
    }

    @Override
    @Transactional(readOnly = false)
    public User save(User user) {
        return repository.save(user);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(User user) {
        repository.delete(user);
    }
    
}